package com.summer.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.summer.commonException.XueChengException;
import com.summer.content.dao.TeachPlanMapper;
import com.summer.content.dao.TeachplanMediaMapper;
import com.summer.content.service.TeachplanMediaService;
import com.summer.dto.BindTeachPlanMediaDto;
import com.summer.pojo.Teachplan;
import com.summer.pojo.TeachplanMedia;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class TeachplanMediaServiceImpl implements TeachplanMediaService {


    @Autowired
    private TeachplanMediaMapper teachplanMediaMapper;

    @Autowired
    private TeachPlanMapper teachPlanMapper;

    /**
     * 课程计划绑定媒资实现：向teachplan_media表添加媒资数据
     *
     * @param bindTeachplanMediaDto dto
     */
    @Override
    public void associationMedia(BindTeachPlanMediaDto bindTeachplanMediaDto) {
        // 数据准备
        String mediaId = bindTeachplanMediaDto.getMediaId();
        Long teachplanId = bindTeachplanMediaDto.getTeachplanId();
        Teachplan teachplan = teachPlanMapper.selectById(teachplanId);
        TeachplanMedia teachplanMedia1 = teachplanMediaMapper.selectOne(new LambdaQueryWrapper<TeachplanMedia>().eq(TeachplanMedia::getTeachplanId, teachplanId));
        if (teachplanMedia1 != null){
            XueChengException.cast("课程计划已绑定了其他视频");
        }
        if (teachplan == null) {
            XueChengException.cast("课程id = " + teachplanId + "的课程计划不存在");
        }
        Integer grade = teachplan.getGrade();
        if(grade!=2){
            XueChengException.cast("只允许第二级教学计划绑定媒资文件");
        }

        Long courseId = teachplan.getCourseId();

        // 封装对象
        TeachplanMedia teachplanMedia = new TeachplanMedia();
        teachplanMedia.setMediaId(mediaId);
        teachplanMedia.setTeachplanId(teachplanId);
        teachplanMedia.setCourseId(courseId);
        teachplanMedia.setMediaFilename(bindTeachplanMediaDto.getFileName());
        teachplanMedia.setCreateDate(LocalDateTime.now());
        // 调用mapper
        int insert = teachplanMediaMapper.insert(teachplanMedia);
        if (insert <= 0) {
            XueChengException.cast("添加课程计划-媒资信息失败");
        }
    }

    @Override
    public void deleteAssociationMedia(Long teachPlanId, String mediaFileId) {
        if (teachPlanId == null) {
            XueChengException.cast("参数teachPlanId为空");
        }
        if (mediaFileId == null) {
            XueChengException.cast("mediaFileId为空");
        }
        // 准备：查看是否存在，存在则删除，否则抛出异常
        TeachplanMedia teachplanMedia = teachplanMediaMapper.selectOne(new LambdaQueryWrapper<TeachplanMedia>()
                .eq(TeachplanMedia::getTeachplanId, teachPlanId)
                .eq(TeachplanMedia::getMediaId, mediaFileId));
        if (teachplanMedia == null){
            XueChengException.cast("课程计划绑定的媒资信息不存在！");
        }
        // 参数校验
        // mapper delete from t... where teachplan_id = ? and media_file_id = ?
        LambdaQueryWrapper<TeachplanMedia> lqw = new LambdaQueryWrapper<>();
        lqw.eq(TeachplanMedia::getTeachplanId, teachPlanId).eq(TeachplanMedia::getMediaId, mediaFileId);
        int delete = teachplanMediaMapper.delete(lqw);
        if (delete <= 0) {
            XueChengException.cast("删除课程计划绑定的媒资信息失败");
        }
    }
}
